package com.hengxinguotong.zhihuichengjian.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Structure;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/27.
 */

public class StructureTreeAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<Structure> structures;
    private int select = -1;

    public StructureTreeAdapter(List<Structure> structures, Context context) {
        inflater = LayoutInflater.from(context);
        this.structures = structures;
    }


    public Structure getSelect() {
        if (select == -1) {
            return null;
        }
        return structures.get(select);
    }

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return structures.size();
    }

    @Override
    public Object getItem(int arg0) {
        return structures.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = inflater
                    .inflate(R.layout.item_structure, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Structure structure = structures.get(position);
        holder.nameTv.setText(structure.getName());
        if (structure.getData().size() == 0) {
            holder.arrowIv.setVisibility(View.GONE);
            holder.checkbox.setVisibility(View.VISIBLE);
            if (position == select) {
                holder.checkbox.setImageResource(R.mipmap.circle_choice_ligt);
            } else {
                holder.checkbox.setImageResource(R.mipmap.circle_choice);
            }
        } else {
            holder.arrowIv.setVisibility(View.VISIBLE);
            holder.checkbox.setVisibility(View.GONE);
        }
        return convertView;

    }

    static class ViewHolder {
        @BindView(R.id.name_tv)
        HXTextView nameTv;
        @BindView(R.id.arrow_iv)
        ImageView arrowIv;
        @BindView(R.id.checkbox)
        ImageView checkbox;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
